package com.walle.nio.threadtest;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadMain {

	public static void process()
	{
		long begin = System.currentTimeMillis() ;
		int num = 5000000 ;
		for( int i=0; i<num; i++ ) { 
			System.err.print( "" );
		}
		
		System.err.println( (System.currentTimeMillis()-begin)+"(ms)");
	}
	
	public static void main(String[] args) {
		
		long begin = System.currentTimeMillis() ;
		
		int taskNum = 10 ;
		
		long b1 = System.currentTimeMillis() ;
		ExecutorService fixedThreadPool = Executors.newFixedThreadPool( taskNum );
		CountDownLatch latch = new CountDownLatch( taskNum ) ;
		System.err.println( "init->"+(System.currentTimeMillis()-b1)+"(ms)");
		
		long b2 = System.currentTimeMillis() ;
		for( int i=0; i< taskNum; i++ )
		{
			fixedThreadPool.execute( new Runnable() {
				
				@Override
				public void run() {
					try
					{
						process() ;
					}
					finally
					{
						latch.countDown(); 
					}
				}
			});
		}
		
		try {
			latch.await();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		System.err.println( "process->"+(System.currentTimeMillis()-b2)+"(ms)");
		
		System.err.println( "result:"+(System.currentTimeMillis()-begin)+"(ms)->threadNum:"+taskNum);
		fixedThreadPool.shutdown();
	}
}
